import time

import pyautogui

imgpath = "./2560_1440/"
count = 0
times = 0
classname = '船舶'


def if_png_exist(name):  # 没啥用的函数
    location = pyautogui.locateOnScreen(image=imgpath + name + '.png')
    if location is None:
        return 0
    else:
        return 1


def refresh():  # 检测选课页面是否刷新出来
    location = pyautogui.locateOnScreen(image=imgpath + '刷新0.png', region=(50, 200, 100, 100))
    # location = pyautogui.locateOnScreen(image=imgpath + '刷新0.png')
    # print(location)
    # Box(left=50, top=228, width=5, height=5)
    if location is not None:
        # print('页面已刷新')
        return 1
    else:
        # print('页面未刷新')
        return -1


def noclass():  # 检测是否有课
    location = pyautogui.locateOnScreen(image=imgpath + '无课.png', region=(500, 200, 300, 100))
    # print(location)
    # Box(left=544, top=233, width=169, height=13)
    if location is None:
        return -1
    else:
        while True:
            pyautogui.moveTo(50, 50)
            location = pyautogui.locateOnScreen(image=imgpath + '查询.png', region=(1000, 100, 300, 100))
            if location is not None:
                # print(location)
                # Box(left=1087, top=158, width=28, height=20)
                x, y = pyautogui.center(location)
                pyautogui.click(x=x, y=y, clicks=1, button='left')
                return 1


def success():  # 检测选课是否成功
    # time.sleep()
    location = pyautogui.locateOnScreen(image=imgpath + '成功.png')
    print(location)
    if location is not None:
        confirm()
        return 1
    else:
        return -1


def full():  # 检测人数是否已满
    # time.sleep()
    location = pyautogui.locateOnScreen(image=imgpath + '满员.png', region=(400, 100, 300, 100))
    # print(location)
    # Box(left=431, top=121, width=188, height=15)
    if location is not None:
        confirm()
        return 1
    else:
        return -1


def confirm():
    while True:
        # pyautogui.moveTo(50, 50)
        location = pyautogui.locateOnScreen(image=imgpath + '确定.png', region=(600, 100, 300, 200))
        time.sleep(0.15)
        if location is not None:
            # print(location)
            # Box(left=727, top=159, width=33, height=20)
            x, y = pyautogui.center(location)
            pyautogui.click(x=x, y=y, clicks=1, button='left')
            return 1


def search():
    location = pyautogui.locateOnScreen(image=imgpath + '查询.png', region=(1000, 100, 300, 100))
    if location is not None:
        print(location)
        # Box(left=1087, top=158, width=28, height=20)
        x, y = pyautogui.center(location)
        pyautogui.click(x=x, y=y, clicks=1, button='left')


'''
a = pyautogui.locateCenterOnScreen(image='网络2.png')
# x, y = pyautogui.center(location)
print(a)
'''


def run():
    print('run()启动')
    search()
    # starttime=time.time()
    while refresh() == -1:
        # endtime=time.time()
        # usetime=endtime-starttime
        # print(usetime)
        pass
        # time.sleep(0.1)
    if noclass() == 1:
        print('暂无课程，刷新中')
        return -1
    print('搜索课程')
    location = pyautogui.locateOnScreen(image=imgpath + '横坐标1.png', region=(1100, 200, 200, 100))
    # print(location)
    # Box(left=1117, top=228, width=90, height=5)

    if location is not None:  # 如果课程存在
        x1, y1 = pyautogui.center(location)

        location = pyautogui.locateOnScreen(image=imgpath + '纵坐标0.png', region=(1100, 200, 100, 300))
        print(location)
        # Box(left=1100, top=228, width=10, height=40)
        # Box(left=637, top=207, width=51, height=13)
        if location is not None:
            x2, y2 = pyautogui.center(location)
        else:
            location = pyautogui.locateOnScreen(image=imgpath + '纵坐标1.png', region=(1100, 200, 100, 300))
            if location is not None:
                x2, y2 = pyautogui.center(location)
            else:
                location = pyautogui.locateOnScreen(image=imgpath + '纵坐标2.png', region=(1100, 200, 100, 300))
                if location is not None:
                    x2, y2 = pyautogui.center(location)
                else:
                    location = pyautogui.locateOnScreen(image=imgpath + '纵坐标3.png', region=(1100, 200, 100, 300))
                    if location is not None:
                        x2, y2 = pyautogui.center(location)
                    else:
                        return -1

        print(x1, y2)
        pyautogui.click(x=x1 + 5, y=y2 + 5, clicks=1, button='left')
        confirm()  # 等待确认
        while True:
            if success() == 1:
                print('选课成功')
                return 1
            elif full() == 1:
                print('所选课程已满')
                return -1

    elif location is None:
        print('未找到' + classname + '课程')
        return -1


starttime = time.time()
while count <= 2:
    if run() == 1:
        print('选课成功，继续搜索')
        count += 1
    else:
        print('选课失败，继续搜索')
    # time.sleep(0.1)
    print('run()执行完毕\n')
    endtime = time.time()
    usetime = endtime - starttime
    times += 1
    print('你已经抢了' + str(times) + '次，耗时' + str(usetime) + '秒，却只抢到了' + str(count) + '门课。')
print('抢课结束，共抢课' + str(count) + '门')
